package com.rbcs.base.item.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rbcs.base.model.Item;
import com.systop.common.modules.dept.model.Dept;
import com.systop.core.ApplicationException;
import com.systop.core.Constants;
import com.systop.core.service.BaseGenericsManager;

/**
 * 预算科目Mananger
 * 
 * @author Nice
 */
@Service
public class ItemManager extends BaseGenericsManager<Item> {

	/** 定义接收树形列表的变化 */
	private List<Item> treeItems = null;

	@Transactional
	public void save(Item item) {
		if (getDao().exists(item, "code")) {
			throw new ApplicationException(item.getCode() + "科目编码已经存在");
		}
		getDao().saveOrUpdate(item);
	}

	/** 为treeItems装填数据 */
	private void buildTreeItems(Integer parentId, Integer topId) {
		List<Item> items = getByParentId(parentId);
		for (Item item : items) {
			if (item.getParent() == null) {
				item.setClassName("");
			} else if (item.getParent().getId().equals(topId)) {
				item.setClassName("");
			} else {
				item.setClassName("child-of-" + item.getParent().getId());
			}
			treeItems.add(item);
			buildTreeItems(item.getId(), topId);
		}
	}

	/**
	 * 根据父ID查询,如果parentId is null,则查询顶级科目
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Item> getByParentId(Integer parentId) {
		if (parentId == null) {
			return query("from Item i where i.parent is null order by i.code");
		} else {
			return query("from Item i where i.parent.id = ? order by i.code",
					parentId);
		}
	}

	/**
	 * 根据名称模糊查询下级科目
	 * 
	 * @param name
	 * @return
	 */
	public List<Item> getSubByName(String name) {
		name = (name == null) ? StringUtils.EMPTY : name;
		name = MatchMode.ANYWHERE.toMatchString(name);
		return query(
				"from Item i where i.name like ? and i.parent is not null",
				name);
	}

	/**
	 * 获得所有下级科目
	 * 
	 * @return
	 */
	public List<Item> getSubs() {
		return query("from Item i where i.parent is not null");
	}

	/**
	 * 获得预算科目的树形列表
	 * 
	 * @return
	 */
	public List<Item> getTreeItems(Integer parentId) {
		treeItems = new ArrayList<Item>();
		buildTreeItems(parentId, parentId);
		return treeItems;
	}

	/**
	 * 获得部门未选择的末端科目
	 * 
	 * @return
	 */
	public List<Item> getUnSelectedByDept(Dept dept) {
		StringBuffer hql = new StringBuffer(200);
		hql.append("from Item i where i.isLeaf = ? ");
		if (dept != null && dept.getId() != null) {
			String itemIds = "0,";
			for (Item i : dept.getItems()) {
				itemIds = itemIds + i.getId() + ",";
			}
			if (itemIds.endsWith(",")) {
				itemIds = itemIds.substring(0, itemIds.length() - 1);
			}
			hql.append("and i.id not in (").append(itemIds).append(") ");
		}
		hql.append("order by i.code");
		return query(hql.toString(), Constants.YES);
	}

	/**
	 * 获得项目类别为选择的末端科目
	 * 
	 * @param ptype
	 * @return
	 */
	public List<Item> getUnSelectedByProjectType() {
		String hql = "from Item i where i.isLeaf = ? and i.projectType is null order by i.code";
		return query(hql, Constants.YES);
	}

	/**
	 * 根据部门序号的获得下级部门涉及到预算科目
	 * 
	 * @param deptSerialNo
	 * @return
	 */
	public List<Item> getByDeptSerialNo(String deptSerialNo) {
		String hql = "select i from Item i join i.depts d where d.serialNo like ? group by (i.id)";
		return query(hql, deptSerialNo + "%");
	}

	/**
	 * 获得公共分摊科目
	 * 
	 * @return
	 */
	public List<Item> getIsShareItems() {
		String hql = "from Item i where i.isShare = ? and i.isLeaf = ? order by i.code";
		return query(hql, new Object[] { Constants.YES, Constants.YES });
	}
}
